package com.hfzy.ihk.web.userCenter.biz;

import com.hfzy.ihk.facade.userCenter.entity.Resource;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.web.oauth.client.utils.UserUtil;
import com.hfzy.ihk.web.userCenter.mapper.ResourceMapper;
import com.hfzy.ihk.web.userCenter.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by YANFA on 2019/2/18.
 */
@Component
public class ResourceBiz {

    @Autowired
    ResourceMapper resourceMapper;

    @Transactional
    public void deleteResource(Long id){
        //删除关系
        resourceMapper.deletePrivHasResourceByResourceId(id);

        resourceMapper.deleteUserHasResourceByResourceId(id);

        resourceMapper.deleteById(id);


    }


    public List<Resource> queryThisUserAllResource(){

        List<Resource> parents = resourceMapper.listTopParents();

        UserInfo user = UserUtil.getLoginUser();
        if(user==null)
            throw  new IllegalArgumentException("无法获取用户");

        List<Long> allResourece = new ArrayList<>();

        List<Long> userHasresourceList =resourceMapper.userHasResource(user.getId().longValue());

        List<Long> privHasResourceList =resourceMapper.privHasResourceByUserId(user.getId().longValue());
        //取并集
        if(userHasresourceList!=null){
            allResourece.addAll(userHasresourceList);
        }
        if(privHasResourceList!=null){
            allResourece.addAll(privHasResourceList);

        }
        if(allResourece==null)
            return null;
        return parents.stream().filter(p->{return allResourece.contains(p.getId().longValue());})
                .sorted(Comparator.comparing((Resource r) -> r.getPriority()))
                .collect(Collectors.toList());

    }

    public List<MenuVo> resourceByParentId(Long parentId){
        List<Resource> permissionsAll = resourceMapper.listParents();
        //当前权限
        UserInfo user = UserUtil.getLoginUser();
        //获取所有菜单权限取并集

        List<Long> allResourece = new ArrayList<>();

        List<Long> userHasresourceList =resourceMapper.userHasResource(user.getId().longValue());

        List<Long> privHasResourceList =resourceMapper.privHasResourceByUserId(user.getId().longValue());
        //取并集
        if(userHasresourceList!=null){
            allResourece.addAll(userHasresourceList);
        }
        if(privHasResourceList!=null){
            allResourece.addAll(privHasResourceList);

        }

        if(allResourece==null)
            return new ArrayList<>();

        List<MenuVo> list =  setMenu(parentId, permissionsAll,allResourece);
        return list;
    }

    public  List<MenuVo> setMenu(Long pId,List<Resource> ResourcesAll,List<Long> currentResources){
        List<MenuVo> list = new ArrayList<>();
        for (Resource re : ResourcesAll) {
            if(pId.intValue()==re.getParentId()&&currentResources.contains(re.getId().longValue())) {
                MenuVo menuVo = new MenuVo();
                menuVo.setId(re.getId().longValue());
                menuVo.setHref(re.getUri());
                menuVo.setIcon(re.getCss());
                menuVo.setSpread(true);
                menuVo.setTitle(re.getLabel());
                menuVo.setChildren(setMenu(re.getId().longValue(),ResourcesAll,currentResources));
                list.add(menuVo);
            }

        }
        return list;

    }

}
